package cn.ys.groupbuy;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapPoi;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.MyLocationConfiguration;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.map.TextOptions;
import com.baidu.mapapi.model.LatLng;

/**
 * Created by shao on 2015/6/8.
 */
public class MapViewActivity extends Activity implements View.OnClickListener{

    private MapView mapView;
    private LocationClient mLocationClient;
    private BaiduMap baiduMap;
    private boolean isFirstLocation=true;
    private TextView addr_text;
    private MyLocationData myLocationData;
    private ImageButton location_but,zoom_in,zoom_out;
    private LatLng mLatLng;
    private float zoom_level=16.0f;
    private float maxZoomLevel;
    private float minZoomLevel;

    //private MapController mapController;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SDKInitializer.initialize(getApplicationContext());
        setContentView(R.layout.map_view);
        mapView=(MapView)findViewById(R.id.map_view);
        mLocationClient=new LocationClient(getApplicationContext());
        isFirstLocation=true;
        addr_text=(TextView)findViewById(R.id.addr_text);
        location_but=(ImageButton)findViewById(R.id.location_but);
        location_but.setOnClickListener(this);
        zoom_in=(ImageButton)findViewById(R.id.zoom_in);
        zoom_in.setOnClickListener(this);
        zoom_out=(ImageButton)findViewById(R.id.zoom_out);
        zoom_out.setOnClickListener(this);
        //清掉地图上的带有百度地图的图片和比例尺
        mapView.removeViewAt(1);
        mapView.removeViewAt(2);

        //mapView.removeViewAt(3);
        //不显示自带的缩放控件
        mapView.showZoomControls(false);
        baiduMap=mapView.getMap();
        //获取最大和最小缩放比例
        maxZoomLevel= baiduMap.getMaxZoomLevel();
        minZoomLevel=baiduMap.getMinZoomLevel();
        //设置缩放比例
        baiduMap.setMapStatus(MapStatusUpdateFactory.zoomTo(zoom_level));
        baiduMap.setOnMapClickListener(new MyOnMapClickListener());
         initLocation();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mapView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mapView.onPause();
    }

    @Override
    protected void onStart() {
        super.onStart();
        baiduMap.setMyLocationEnabled(true);
        if(!mLocationClient.isStarted()){

            mLocationClient.start();
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        baiduMap.setMyLocationEnabled(false);
        mLocationClient.stop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mapView.onDestroy();
    }

    private void initLocation(){
        mLocationClient.registerLocationListener(new MyLocationLisetener());
        LocationClientOption option = new LocationClientOption();
        option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);
        option.setOpenGps(true);
        //option.setAddrType("all");
        //option.setIsNeedAddress(true);
        option.setCoorType("bd09ll");//返回的定位结果是百度经纬度，默认值gcj02
        option.setScanSpan(5000);//设置发起定位请求的间隔时间为5000ms
        option.setNeedDeviceDirect(true);
        option.setIsNeedAddress(true);
        mLocationClient.setLocOption(option);
    }

    @Override
    public void onClick(View v) {
        if (v == location_but) {
            Toast.makeText(MapViewActivity.this,"返回当前位置",Toast.LENGTH_SHORT).show();
            baiduMap.animateMapStatus(MapStatusUpdateFactory.newLatLng(mLatLng));
        }
        //使用自定义的放大控件
        if (v == zoom_in) {
            zoom_level++;
            zoom_out.setEnabled(true);
            zoom_out.setImageResource(R.mipmap.ic_zoom_out);
            if(zoom_level<=maxZoomLevel){
                baiduMap.setMapStatus(MapStatusUpdateFactory.zoomTo(zoom_level));
                if(zoom_level==maxZoomLevel){
                    zoom_in.setEnabled(false);
                    zoom_in.setImageResource(R.mipmap.ic_zoom_in_unable);
                }
            }

        }
        //使用自定义的缩小控件
        if(v==zoom_out){
            zoom_level--;
            zoom_in.setEnabled(true);
           // zoom_in.setBackgroundResource(R.mipmap.ic_zoom_in);
            zoom_in.setImageResource(R.mipmap.ic_zoom_in);
            if(zoom_level>=minZoomLevel){
                baiduMap.setMapStatus(MapStatusUpdateFactory.zoomTo(zoom_level));
                if(zoom_level==minZoomLevel){
                    zoom_out.setEnabled(false);
                    zoom_out.setImageResource(R.mipmap.ic_zoom_out_unable);
                }
            }
        }
    }

    class MyLocationLisetener implements BDLocationListener{

        @Override
        public void onReceiveLocation(BDLocation bdLocation) {
           myLocationData= new MyLocationData.Builder()
            .accuracy(bdLocation.getRadius())
            .latitude(bdLocation.getLatitude())
            .longitude(bdLocation.getLongitude())
            .build();
            baiduMap.setMyLocationData(myLocationData);
            //当前位置的图标
            BitmapDescriptor mCurrentMarker= BitmapDescriptorFactory.fromResource(R.mipmap.ic_maps_indicator_current_position);
            MyLocationConfiguration configuration=new MyLocationConfiguration(MyLocationConfiguration.LocationMode.NORMAL,true,mCurrentMarker);
            baiduMap.setMyLocationConfigeration(configuration);
            addr_text.setText(bdLocation.getAddrStr());
            if(isFirstLocation){
                isFirstLocation=false;
                mLatLng=new LatLng(bdLocation.getLatitude(),bdLocation.getLongitude());
               //定位到当前位置
                baiduMap.animateMapStatus(MapStatusUpdateFactory.newLatLng(mLatLng));
            }

        }
    }

    //点击地图上某点的监听
    class MyOnMapClickListener implements BaiduMap.OnMapClickListener{

        @Override
        public void onMapClick(LatLng latLng) {
          baiduMap.clear();
/*          BitmapDescriptor bitmapMarker=BitmapDescriptorFactory.fromResource(R.mipmap.maker);
            OverlayOptions overlayOptions=new MarkerOptions()
                    .position(latLng)
                    .icon(bitmapMarker);

            baiduMap.addOverlay(overlayOptions);*/
        }

        @Override
        public boolean onMapPoiClick(MapPoi mapPoi) {
            baiduMap.clear();
           // Toast.makeText(MapViewActivity.this,"mapPoi.getName() = "+mapPoi.getName()+" position = "+mapPoi.getPosition(),Toast.LENGTH_SHORT).show();
            //添加覆盖地图上的文本信息
            OverlayOptions textOverlay=new TextOptions()
                    .text(mapPoi.getName())
                    .bgColor(0xAAFFFF00)
                    .fontColor(0xFFFF00FF)
                    .fontSize(25)
                    .position(mapPoi.getPosition());
            baiduMap.addOverlay(textOverlay);
            //添加覆盖地图上的图标信息
            BitmapDescriptor bitmapMarker=BitmapDescriptorFactory.fromResource(R.mipmap.maker);
            OverlayOptions overlayOptions=new MarkerOptions()
                    .position(mapPoi.getPosition())
                    .icon(bitmapMarker);
            baiduMap.addOverlay(overlayOptions);
            baiduMap.animateMapStatus(MapStatusUpdateFactory.newLatLng(mapPoi.getPosition()));
            return false;
        }
    }

}
